package com.studentServer.controller;


import com.studentServer.bean.Student;
import com.studentServer.dao.StudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/student")
@RestController
public class StudentController {
    @Autowired
    StudentDao studentDao;

    @ResponseBody
    @RequestMapping("/login")
    public Map login(@RequestBody Student student) {
        Map map = new HashMap();

        Student student1 = studentDao.findOne(Example.of(student)).orElse(null);

        if (student1 != null) {
            map.put("success", true);
            map.put("message", "登录成功!");
            map.put("data", student1);
        } else {
            map.put("success", false);
            map.put("message", "登录失败!");
            map.put("data", null);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/register")
    public Map register(@RequestBody Student student) {
        Map map = new HashMap();
        Student student1 = studentDao.saveAndFlush(student);

        if (student1 != null) {
            map.put("success", true);
            map.put("message", "注册成功!");
            map.put("data", student1);
        } else {
            map.put("success", false);
            map.put("message", "注册失败!");
            map.put("data", null);
        }
        return map;
    }
}
